package com.syqc.monitor.ui.carinfo

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.Driver
import com.syqc.entity.GpsDetail
import com.syqc.entity.NameValue
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper

/**
 *@Author pc_hfjs
 *@Date 2022/3/17 16:13
 *@Desc
 */
class CarInfoViewModel : AutoDisposeViewModel() {

    var carId = ""

    private val _remindLive = MutableLiveData<MutableList<NameValue>>()
    val remindLive: LiveData<MutableList<NameValue>> = _remindLive

    private val _sgnLive = MutableLiveData<MutableList<NameValue>>()
    val sgnLive: LiveData<MutableList<NameValue>> = _sgnLive

    private val _locationLive = MutableLiveData<GpsDetail>()
    val locationLive: LiveData<GpsDetail> = _locationLive

    private val _carPhotoLive = MutableLiveData<MutableList<String>>()
    val carPhotoLive: LiveData<MutableList<String>> = _carPhotoLive

    private val _tmnPhotoLive = MutableLiveData<MutableList<String>>()
    val tmnPhotoLive: LiveData<MutableList<String>> = _tmnPhotoLive

    private val _drvPhotoLive = MutableLiveData<MutableList<String>>()
    val drvPhotoLive: LiveData<MutableList<String>> = _drvPhotoLive


    //司机数据
    val driverInfo = mutableListOf<Driver>()

    fun getSgnRemind(type: String) {
        ServiceHelper.getRepository().getCarSgnRemind(carId, type).autoDispose(this)
            .subscribe({
                when (type) {
                    "0" -> _sgnLive.postValue(it.sgnStts)
                    "1" -> _remindLive.postValue(it.reminds)
                }
            }, error)
    }

    /**
     * 获取车辆信息
     */
    fun getGprR() {
        ServiceHelper.getRepository().getGpsRInfo("", carId, simple = false, filter = false)
            .autoDispose(this)
            .subscribe({
                val carInfo = it[0]
                val drvName=carInfo.drvName
                if (drvName.isNotEmpty() && carInfo.drvPhone.isNotEmpty()) {
                    driverInfo.clear()
                    val names  = drvName.split(",")
                    val phones = carInfo.drvPhone.split(",")
                    for (i in phones.indices) {
                        driverInfo.add(Driver(names[i], phones[i]))
                    }
                }
                 _locationLive.postValue(carInfo)
            }, error)
    }

    fun gitPic(type: String) {
        ServiceHelper.getRepository().getPic(carId, type).autoDispose(this)
            .subscribe({
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                } else {
                    when (type) {
                        "car" -> _carPhotoLive.postValue(it)
                        "tmn" -> _tmnPhotoLive.postValue(it)
                        "drv" -> _drvPhotoLive.postValue(it)
                    }
                }
            }, error)
    }

}